package algorithm.sort.shell;

import algorithm.sort.base.ArrayUtils;

public class ShellSort {
    public static void sort(int[] data) {
        for (int gap = data.length/2; gap > 0; gap = gap / 2) {
            for (int i = gap; i < data.length; i++) {
                for (int j = i; j >= 0; j -= gap) {
                    int pre = j - gap;
                    if(pre>=0){
                        System.out.println("\t\t\t"+ArrayUtils.toString(data)+ String.format("   doCompare:  %2d-%2d  [%2d]--[%2d]", j, pre, data[j],data[pre]));
                    }
                    if (pre >= 0 && data[j] < data[pre]) {
                        ArrayUtils.swap(data, j, pre);
                        System.out.println("\t\t\t"+ArrayUtils.toString(data)+ String.format("     doSwap:     %2d-%2d  [%2d]--[%2d]", j, pre, data[j],data[pre]));
                    } else{
                        break;
                    }
                }
                System.out.println("\t\t"+ArrayUtils.toString(data));
            }
            System.out.println("\t"+ArrayUtils.toString(data));
        }
    }
}
